<?php
namespace App\User\Services;

use App\BaseService;
use App\User\Models\Archive;
use Exception;
use App\Handles\GeneratedNumber;
use Illuminate\Support\Facades\DB;
use App\Handles\CrmRpcHandle;

class ArchiveService extends BaseService
{
    use GeneratedNumber, CrmRpcHandle;

    /**
     * User: Virus
     * DateTime: 2021/6/21 13:49
     * Describe: 设置数据（预留方法）
     * @param $data
     * @return mixed
     */
    protected function setData($data)
    {


        return $data;
    }

    /**
     * User: Virus
     * DateTime: 2021/6/21 10:03
     * Describe: 创建档案
     * Version: v1.0.0
     * @throw $data Array
     * @throws Exception
     */
    public function create($data)
    {
        DB::beginTransaction();
        try {
            $data = $this->setData($data);
            // 生成病历号
            $data['medical_sn'] = $this->setMedicalRecordSn();
            // 验证重复
            $unique = Archive::where('id_card', $data['id_card'])->count();
            if($unique){
                throw new Exception('档案已存在！');
            }
            // 同步用户信息
            $user = $this->getUserInfoHandle($data['id_card']);
            if($user){
                $data['user_id'] = $user['id'];
            }else{
                $data['user_id'] = 0;
            }
            $doctor_id = request()->input('doctor_id', false);
            if(!$doctor_id){
                throw new Exception('缺少参数！');
            }
            // 添加
            $archive = Archive::create($data);
            if(!$archive){
                throw new Exception('创建个人档案失败！');
            }
            DB::commit();
            return $archive;
        }catch (Exception $exception){
            DB::rollback();
            throw new Exception($exception->getMessage());
        }
    }


    /**
     * User: Virus
     * DateTime: 2021/6/21 15:41
     * Describe: 用户详情
     * Version: v1.0.0
     * @param $condition
     * @return mixed
     * @throws Exception
     */
    public function getInfo($condition)
    {

        $archive = Archive::select(['id', 'user_id', 'doctor_id', 'card_type', 'id_card', 'birthday', 'age', 'tel'])
            ->where(function ($table) use ($condition){
                $table->orWhere('id', $condition);
                $table->orWhere('name', $condition);
                $table->orWhere('id_card', $condition);
                $table->orWhere('medical_sn', $condition);
            })->first();

        // 查询医生信息
        $doctor = $this->getDoctorInfoHandle($archive->doctor_id);
        // 查询用户信息
        if($archive->user_id){
            $user = $this->getUserInfoHandle($archive->user_id);
        }else{
            $user = [];
        }
        $archive->docter = $doctor;
        $archive->user = $user;

        return $archive;
    }

    /**
     * User: Virus
     * DateTime: 2021/6/21 19:30
     * Describe: 获取个人档案列表
     * Version: v1.0.0
     */
    public function getList($data, $limit)
    {
        $select = [
            'id', 'name', 'age', 'sex', 'card_type', 'id_card', 'tel', 'address_province',
            'address_city', 'address_county', 'address_detail', 'birthday', 'created_at'
        ];
        Archive::closeAttributeSwitch(['profession_type_text']);
        return Archive::select($select)->paginate($limit);
    }

    /**
     * User: Virus
     * DateTime: 2021/6/22 11:13
     * Describe: 修改身份证号详情
     * Version:
     * @param $data
     * @return mixed
     * @throws Exception
     */
    public function update($data)
    {
        DB::beginTransaction();
        try {
            $archive = Archive::find($data['id']);
            if(!$archive){
                throw new Exception('档案不存在或已删除！');
            }

            $data = $this->setData($data);
            // 验证重复
            $unique = Archive::where('id_card', $data['id_card'])->where('id', '!=' , $archive->id)->count();
            if($unique){
                throw new Exception('身份证号重复！');
            }

            // 添加
            $res = $archive->update($data);
            if(!$res){
                throw new Exception('修改个人档案失败！');
            }
            DB::commit();
            return $archive;
        }catch (Exception $exception){
            DB::rollback();
            throw new Exception($exception->getMessage());
        }
    }

    /**
     * 删除个人档案信息
     */
    public function delete($id)
    {
        $archive = Archive::find($id);
        if(!$archive){
            throw new Exception('数据不存在或已删除！');
        }
        if(!$archive->delete()){
            throw new Exception('删除失败！');
        }
        return true;
    }
}
